package com.xpqh.ksg.base;

import com.xpqh.ksg.common.enums.result.BizExceptionEnum;
import com.xpqh.ksg.common.enums.result.BizSuccessEnum;
import com.xpqh.ksg.common.utils.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lzh
 * @date 2019.05.12
 *
 */
@Slf4j
@Component
public class BaseController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    /**
     * 从请求头 Authorization 中获取 token
     */
    protected String getToken() {
        String authorization = request.getHeader("Authorization");
        if (authorization == null || !authorization.startsWith("Bearer ")) {
            return null;
        }
        return authorization.substring(7); // 去掉 "Bearer " 前缀
    }

    /**
     * 从 token 中解析 userId
     */
    protected int getUserId() {
        String token = getToken();
        if (token == null) {
            return 0;
        }
        return Integer.parseInt(jwtTokenUtil.getUsernameFromToken(token));
    }

    protected BaseResponseDTO renderResultOk() {
        return new BaseResponseDTO(null, BizSuccessEnum.OK.getCode(), BizSuccessEnum.OK.getMessage());
    }

    protected BaseResponseDTO renderResultOk(Object data) {
        return new BaseResponseDTO(data, BizSuccessEnum.OK.getCode(), BizSuccessEnum.OK.getMessage());
    }

    protected BaseResponseDTO renderResult(Object data, Integer code, String message) {
        log.debug("返回响应: code={}, message={}, data={}", code, message, data);
        return new BaseResponseDTO(data, code, message);
    }

    protected BaseResponseDTO renderResult(Integer code, String message) {
        log.debug("返回响应: code={}, message={}", code, message);
        return new BaseResponseDTO(code, message);
    }

    protected BaseResponseDTO renderResult(Object data, BizSuccessEnum bizSuccessEnum) {
        return renderResult(data, bizSuccessEnum.getCode(), bizSuccessEnum.getMessage());
    }

    protected BaseResponseDTO renderResult(BizSuccessEnum bizSuccessEnum) {
        return renderResult(null, bizSuccessEnum.getCode(), bizSuccessEnum.getMessage());
    }

    protected BaseResponseDTO renderResult(Object data, BizExceptionEnum bizExceptionEnum) {
        return renderResult(data, bizExceptionEnum.getCode(), bizExceptionEnum.getMessage());
    }

    protected BaseResponseDTO renderResult(BizExceptionEnum bizExceptionEnum) {
        return renderResult(null, bizExceptionEnum.getCode(), bizExceptionEnum.getMessage());
    }

    protected HttpServletRequest getHttpServletRequest() {
        if (request == null) {
            throw new IllegalStateException("无法获取当前请求对象");
        }
        return request;
    }

    protected HttpServletResponse getHttpServletResponse() {
        if (response == null) {
            throw new IllegalStateException("无法获取当前响应对象");
        }
        return response;
    }

    protected String getPara(String name) {
        return getHttpServletRequest().getParameter(name);
    }

    protected void setAttr(String name, Object value) {
        getHttpServletRequest().setAttribute(name, value);
    }

    protected Object getAttr(String name) {
        return getHttpServletRequest().getAttribute(name);
    }

    protected String getReqIpAddress() {
        HttpServletRequest request = getHttpServletRequest();
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
